var getTitelsUrl = "titels.php?schrijver=";
var xmlHttpGetTitels = createXmlHttpRequestObject();

function showTitles(schrijver)
{
    try 
    {  
        if (xmlHttpGetTitels.readyState == 4 ||  
            xmlHttpGetTitels.readyState == 0)  
            {     
            xmlHttpGetTitels.open("GET",  
                getTitelsUrl + encode(schrijver), true); 
            xmlHttpGetTitels.onreadystatechange = handleGettingTitels;  
            xmlHttpGetTitels.send(null); 
        } 
    } 
    catch(e)
    { 
        displayError("Can't connect to server:\n" + e.toString()); 
    } 
//}
} 
    
/* handles the server's response containing the suggestions for the requested keyword  */ 
function handleGettingTitels()  
{ 
    //if the process is completed, decide what to do with the returned data 
    if (xmlHttpGetTitels.readyState == 4)  
    { 
        // only if HTTP status is "OK" 
        if (xmlHttpGetTitels.status == 200)  
        {  
            try 
            { 
                // process the server's response 
                showTitels();
            } 
            catch(e) 
            { 
                // display the error message 
                displayError(e.toString());  
            }   
        }  
        else 
        { 
            displayError("There was a problem retrieving the data:\n" +  
                xmlHttpGetTitels.statusText); 
        }        
    } 
}

function showTitels() 
{ 
    // retrieve the server's response  
    var response = xmlHttpGetTitels.responseText; 
    
    // server error? 
    if (response.indexOf("ERRNO") >= 0  
        || response.indexOf("error:") >= 0 
        || response.length == 0) 
        throw(response.length == 0 ? "Void server response." : response); 
    
    // retrieve the document element 
    response = xmlHttpGetTitels.responseXML.documentElement; 
    
    // initialize the new array of functions' names 
    titleArray = new Array(); 
    
    // check to see if we have any results for the searched keyword
    if(response.childNodes.length) 
    { 
        /* we retrieve the new functions' names from the document element as an array */ 
        titleArray = xmlToArray(response.getElementsByTagName("name"));        
    } 
    
    ul = "<ul>";
    for (title in titleArray)
    {
        ul += "<li>" + titleArray[title] + "</li>";
    }
    ul += "</ul>";
    
    var showResults = document.getElementById("showResults");
    showResults.innerHTML = ul;
}
